package cn.edu.ctbu.lablist.webapi;

import cn.edu.ctbu.lablist.core.PageUtils;
import cn.edu.ctbu.lablist.domain.Score;
import cn.edu.ctbu.lablist.domain.Student;
import cn.edu.ctbu.lablist.domain.Teacher;
import cn.edu.ctbu.lablist.service.ScoreService;
import cn.edu.ctbu.lablist.service.StudentService;
import cn.edu.ctbu.lablist.service.TeacherService;
import cn.edu.ctbu.lablist.utils.RUtil;
import cn.edu.ctbu.lablist.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/webapi/teacher")
public class TeacherRestController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/gett/{no}")//根据教师编号查询教师信息
    public R gett(@PathVariable String no){
        Teacher teacher = teacherService.findByNo(no);
        return RUtil.success(teacher);
    }
    @GetMapping("/getall")//查询所有教师信息
    public R<Teacher> getall(){
        List<Teacher> teachers = teacherService.findAll();
        return RUtil.success(teachers);
    }
    /**
     * 分页，读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                       @RequestParam(value = "size",defaultValue = "10") Integer size,
                       @RequestParam(value = "name",defaultValue = "") String name){
        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.ASC,"id");
        Page<Teacher> teacherPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable = PageRequest.of(page,size,sort);
            teacherPage = teacherService.findAll(pageable);
        }
        else{
            Teacher teacher = new Teacher();
            teacher.setName(name);
            Pageable pageable = PageRequest.of(0,10,sort);
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Teacher> example = Example.of(teacher,matcher);
            teacherPage = teacherService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
    @PostMapping("/insert")//插入教师信息
    public R insert(@Valid Teacher teacher, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            log.info("新增教师出错了!{}",errorMsg);
            return RUtil.error(-10,errorMsg);
        }
        teacherService.insert(teacher);
        Long myid = Long.valueOf(teacher.getId());

        return RUtil.success(myid.intValue());
    }
    @PutMapping("/update")//修改教师信息
    public R<Teacher> update(Teacher teacher){
         teacherService.update(teacher);
        return RUtil.success(teacher);
    }
    @DeleteMapping("/delete/{id}")//删除教师信息
    public void delete(@PathVariable Integer id){
        teacherService.delete(id);
    }
}
